package org.nerve.android.ui.dialog;

import java.lang.reflect.Method;

import org.nerve.android.ui.dialog.DialogHelper.ConfirmListener;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;



/**
 * @项目名称 :nerveAndroid
 * @文件名称 :ConfirmDialog.java
 * @所在包 :org.nerve.ui.dialog
 * @功能描述 :
 *	选择对话框
 *
 *	加载指定layout，并搜索指定tag 的元素，然后对搜索到的View加一个点击事件
 *	点击后，返回的是这个View所在ViewGroup 出现的序号
 *
 * @创建者 :集成显卡	1053214511@qq.com
 * @公司：广西可诺科技有限公司(http://www.chonour.com/)
 * @创建日期 :2013-6-6
 * @修改记录 :
 */
public class SelectDialog {
	
	protected Context context;
	protected Dialog dialog;
	
	protected int layout;
	protected int iconLayout;
	protected String tag;
	protected int curSelect;
	
	protected OnClickListener clickListener;
	
	protected ConfirmListener listener;
	
	protected ViewGroup rootView;
	
	public SelectDialog(Context context, int layout){
		this.context = context;
		this.layout = layout;
		
		clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("on click" + v);
				
				int index = v.getId();
				String value = null;
				
				try{
					Class<? extends View> clazz = v.getClass();
					Method m = clazz.getDeclaredMethod("getText");
					if(m != null)
						value = ((Editable)m.invoke(v)).toString();
				}catch(Exception e){
					e.printStackTrace(System.out);
				}
				
				if(listener != null)
					listener.onConfirmClick(index, value);
				
				dialog.dismiss();
			}
		};
	}
	
	public void setConfirmListener(ConfirmListener listener){
		this.listener = listener;
	}
	
	/**
	 * 创建对话框
	 */
	protected void createDialog(){
		ViewGroup view = (ViewGroup)View.inflate(context, layout, null);	
		
		dialog = new Dialog(context);
		dialog.show();
		Window win = dialog.getWindow();
		//将dialog的背景透明化
		win.setBackgroundDrawable(new ColorDrawable(0));
		
		win.setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		win.setContentView(view);
		
		rootView = view;
		refreshList();
	}
	
	private void refreshList(){
		ViewGroup view = rootView;
		
		Drawable arrowDrawable = null;
		if(iconLayout > 0){
			arrowDrawable = context.getResources().getDrawable(iconLayout);
			arrowDrawable.setBounds(0, 0, arrowDrawable.getMinimumWidth(), arrowDrawable.getMinimumHeight());
		}
		
		int index = 0;
		for(int i=0;i<view.getChildCount();i++){
			View v = view.getChildAt(i);
			Object obj = v.getTag();
			
			if(obj != null && obj.equals(tag)){
				System.out.println("index = " + index + ", curSelect = " + curSelect);
				
				if(v instanceof TextView){
					if(index == curSelect){
						((TextView)v).setCompoundDrawables(null, null, arrowDrawable, null);
					}else {
						((TextView)v).setCompoundDrawables(null, null, null, null);
					}
				}
				
				v.setId(index++);
				v.setOnClickListener(clickListener);
			}
		}
	}
	
	public void show(){
		if(dialog == null)
			createDialog();
		else{
			refreshList();
			dialog.show();
		}
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public void setCurSelect(int curSelect) {
		this.curSelect = curSelect;
	}
	
	public void setIcon(int id){
		this.iconLayout = id;
	}
}